package com.common.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.dao.PrivilegeDAO;
import com.common.dao.RoleDAO;
import com.common.dao.RolePrivilegeDAO;
import com.common.dto.RoleDTO;
import com.common.dto.RolePrivilegeDTO;
import com.common.service.RoleService;
import com.common.service.base.BaseService;
import com.common.util.ConstantDefine;
import com.common.util.StringUtil;
import com.github.pagehelper.PageInfo;

import tk.mybatis.mapper.entity.Example;

@Service
public class RoleServiceImpl extends BaseService<RoleDTO> implements RoleService {

	@Autowired
	private RoleDAO roleDao;
	@Autowired
	private PrivilegeDAO privilegeDao;
	@Autowired
	private RolePrivilegeDAO rolePrivilegeDao;




	@Override
	public Map<String, Object> check(RoleDTO entity) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Map<String, Object> checkUpdate(RoleDTO entity) {
        return null;
    }


    /**
	 * 2017/07/10
	 */
	@Override
	public Map<String, Object> saveRole(RoleDTO role) {
		Map<String,Object> map = new HashMap<>();
        String roleCode = StringUtil.getUUID().substring(2, 8);
        role.setRoleCode(roleCode);
        roleDao.insertSelective(role);

        //权限
        List<String> functionCodeList = role.getFunctionCodeList();
        if(functionCodeList != null){
            RolePrivilegeDTO rolePrivilege = new RolePrivilegeDTO();
            rolePrivilege.setRoleCode(roleCode);
            if(functionCodeList.size() != 0){
                for(Iterator iterator = functionCodeList.iterator(); iterator.hasNext();){
                    String functionCode = (String) iterator.next();
                    rolePrivilege.setFunctionCode(functionCode);
                    rolePrivilegeDao.insertSelective(rolePrivilege);
                }
            }
        }
		map.put(ConstantDefine.FLAG,true);
		return map;
	}

	/**
	 * 2017/07/10
	 */
	@Override
	public Map<String, Object> updateRole(RoleDTO rolePara) {
		Map<String,Object> map = new HashMap<>();
		roleDao.updateByPrimaryKeySelective(rolePara);

		RoleDTO role = roleDao.selectByPrimaryKey(rolePara.getId());
        RolePrivilegeDTO rolePrivilege = new RolePrivilegeDTO();            
        rolePrivilege.setStatus("0");
        rolePrivilege.setModifyTime(rolePara.getModifyTime());
        rolePrivilege.setModifier(rolePara.getModifier());
        
        Example example = new Example(RolePrivilegeDTO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleCode", role.getRoleCode());
        criteria.andEqualTo("status","1");
        //rolePrivilegeDao.delete(rolePrivilege);
        //rolePrivilege更新内容 ，example为更新条件,将已有权限全部无效掉
        rolePrivilegeDao.updateByExampleSelective(rolePrivilege, example);
        //更新权限
        List<String> functionCodeList = rolePara.getFunctionCodeList();
        if(functionCodeList != null && functionCodeList.size() != 0 ){
            for(Iterator i = functionCodeList.iterator(); i.hasNext();){
                String functionCode = (String) i.next();
                rolePrivilege = new RolePrivilegeDTO();
                rolePrivilege.setRoleCode(role.getRoleCode());
                rolePrivilege.setFunctionCode(functionCode);
                rolePrivilege.setStatus("1");
                rolePrivilege.setCreateTime(role.getModifyTime());
                rolePrivilege.setCreator(role.getModifier());
                rolePrivilege.setModifier(role.getModifier());
                rolePrivilege.setModifyTime(role.getModifyTime());
                rolePrivilegeDao.insertSelective(rolePrivilege);
            }
        }

		map.put(ConstantDefine.FLAG,true);
		return map;
	}

	/**
	 * 2017/07/10
	 */
	@Override
	public Map<String,Object> deleteRoles(List<String> list) {
		Map<String,Object> map = new HashMap<String,Object>();
        for(String id :list){
            roleDao.deleteByPrimaryKey(id);
        }
		map.put(ConstantDefine.FLAG,true);
		return map;
	}

    /**
     * 2017/07/11
     */
    @Override
    public RoleDTO updateInit(String id) {
        RoleDTO roleDTO = roleDao.selectByPrimaryKey(id);
        String roleCode = roleDTO.getRoleCode();
        List<String> functionCodeList = rolePrivilegeDao.selectFunctionCodeByRoleCode(roleCode);
        roleDTO.setFunctionCodeList(functionCodeList);
        return roleDTO;
    }

    @Override
    public Map<String, Object> deleteRole(RoleDTO entity) {
        Map<String, Object> map = new HashMap<>();
        roleDao.deleteByPrimaryKey(entity);
        map.put(ConstantDefine.FLAG,true);
        return map;
    }

    //
    @Override
    public Map<String, Object> selectPaging(RoleDTO entity) {
        List<RoleDTO> list = roleDao.selectPaging(entity);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(ConstantDefine.ROWS, list);
        return resultMap;
    }

    @Override
    public List<RoleDTO> selectAll() {
        return roleDao.selectAll();
    }


    @Override
	public PageInfo<RoleDTO> selectCustomPaging(RoleDTO entity) {
		// TODO Auto-generated method stub
		return null;
	}
}
